package test.baojia.act.check;

import com.alibaba.fastjson.JSONObject;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import test.baojia.act.Act;

import java.util.List;
import java.util.Properties;

public class FormCheckWith extends Check {

    protected final static String RISK_TABLE = "#classTBody > tr:nth-child(%s) > td:nth-child(%s)";
    protected final static String RISK_TABLE_LINKA = "#classTBody > tr:nth-child(%s) > td:nth-child(4) > a > span";

    @Override
    public void run(JSONObject context, WebDriver driver, Properties resource, JSONObject parentContext) throws Exception {
        super.run(context, driver, resource, parentContext);
        //验证项目号
        final String globalProperty = (String) this.context.get(OPERATION.byGlobalProperty.toString());
        String globalValue = (String) resource.get(globalProperty);
        WebElement webElement = driver.findElement(By.id("projectCode"));
        this.newWebDriverWait(null).until(
                ExpectedConditions.textToBePresentInElementValue(By.id("projectCode"), globalValue)
        );
        //获取基本信息id
        final List<String> baseInfoIdList = (List<String>) this.context.get("baseInfoName");
        final List<String> byValuesList = (List<String>) this.context.get("byValues");

        //验证基本信息
        for (int i = 1; i <= baseInfoIdList.size(); i++) {
            String id = baseInfoIdList.get(i - 1);
            String value = byValuesList.get(i - 1);

            if("businessArea".equals(id)){
                this.newWebDriverWait(null).until(
                        ExpectedConditions.textToBePresentInElementValue(By.cssSelector("#fm > fieldset:nth-child(34) > div:nth-child(2) > div:nth-child(6) > input"), value)
                );
                continue;
            }

            //中介名称问题跳过中介名称校验
            if("agentName".equals(id)){
                continue;
            }
            this.newWebDriverWait(null).until(
                    ExpectedConditions.textToBePresentInElementValue(By.id(id), value)
            );
        }

        //验证险种信息。
        final List<Object> riskTableList = (List<Object>) this.context.get("itemRisks");
        for (int i = 1; i <= riskTableList.size(); i++) {
            List<String> oneRisk = (List<String>) riskTableList.get(i - 1);
            for (int j = 1; j <= oneRisk.size(); j++) {
                String riskValue = oneRisk.get(j - 1);
                //判断险种表格“审核”
                if(j==3){
                    String selectorRiskAudit = String.format(RISK_TABLE_LINKA, i, j+1);
                    this.newWebDriverWait(null).until(
                            ExpectedConditions.textToBePresentInElementLocated(By.cssSelector(selectorRiskAudit), riskValue)
                    );
                    continue;
                }
                String selectorRisk = String.format(RISK_TABLE, i, j+1);
                this.newWebDriverWait(null).until(
                        ExpectedConditions.textToBePresentInElementLocated(By.cssSelector(selectorRisk), riskValue)
                );
            }
        }
    }
}
